<?php
namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Order extends Api
{
  protected $noNeedLogin = [];
  protected $noNeedRight = ['*'];


  public function scheduled()
  {
    $params = request()->post();
    $room = \app\admin\model\Room::find($params['room_id']);
    if (empty($room)|| $room['status']!="未使用"){
      $this->error("预定失败,房屋当前状态为：{$room->status}");
    }
    $data = \app\admin\model\Order::create([
      "status"=>'预定中',
      "user_id"=>$this->auth->id,
      "start_time"=>$params['start_time'],
      "end_time"=>$params['end_time'],
      "people"=>$params['people'],
      "tel"=>$params['tel'],
      "room_id"=>$params['room_id'],
      "price"=>$params['price'],
      "total"=>$params['total'],
    ]);
    \app\admin\model\Room::update(['status'=>"预定中"],["id"=>$params['room_id']]);
    $this->success('预定成功',$data);
  }

  public function list()
  {
    $data = \app\admin\model\Order::with(['room'])->where(['user_id'=>$this->auth->id]);
    if ($status = $this->request->post('status')){
      $data = $data->where('status',$status);
    }
    $data= $data->select();
    $this->success('获取成功',$data);
  }
  public function update()
  {
    $id =  request()->post('id');
    $status = request()->post('status');

    $order = \app\admin\model\Order::find($id);
    if ($order->status!="预定中"){
      $this->error('确认失败,当前房屋状态有误');
    }
    $order->status = $status;
    $order->save();
    $this->success('操作成功',$order);
  }
}
